package com.cxhc.system.service.app.impl;

import java.util.List;
import com.cxhc.common.utils.DateUtils;
import com.cxhc.system.domain.app.CmApartmentInfo;
import com.cxhc.system.domain.app.CmHouseholdInfo;
import com.cxhc.system.domain.app.CmRoomInfo;
import com.cxhc.system.mapper.app.CmApartmentInfoMapper;
import com.cxhc.system.mapper.app.CmHouseholdInfoMapper;
import com.cxhc.system.mapper.app.CmRoomInfoMapper;
import com.cxhc.system.service.app.ICmHouseholdInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 住户Service业务层处理
 * 
 * @author Dhq
 * @date 2024-11-14
 */
@Service
public class CmHouseholdInfoServiceImpl implements ICmHouseholdInfoService
{
    @Autowired
    private CmHouseholdInfoMapper cmHouseholdInfoMapper;

    @Autowired
    private CmApartmentInfoMapper cmApartmentInfoMapper;

    @Autowired
    private CmRoomInfoMapper cmRoomInfoMapper;

    /**
     * 查询住户
     * 
     * @param id 住户主键
     * @return 住户
     */
    @Override
    public CmHouseholdInfo selectCmHouseholdInfoById(Long id)
    {

        CmHouseholdInfo cmHouseholdInfo = cmHouseholdInfoMapper.selectCmHouseholdInfoById(id);
        CmRoomInfo cmRoomInfo = cmRoomInfoMapper.selectCmRoomInfoByApartIdAndRoomNo(Long.valueOf(cmHouseholdInfo.getUserApartment()), cmHouseholdInfo.getUserRoom());
        if(ObjectUtils.isEmpty(cmRoomInfo)){
            throw new RuntimeException("该租户未绑定房间");
        }
        cmHouseholdInfo.setUserFloor(cmRoomInfo.getRoomFloor());
        cmHouseholdInfo.setControlName(cmRoomInfo.getLegal());
        cmHouseholdInfo.setControlIdCard(cmRoomInfo.getLegalCard());
        cmHouseholdInfo.setControlTel(cmRoomInfo.getLegalTel());
        return cmHouseholdInfo;
    }

    /**
     * 查询住户列表
     * 
     * @param cmHouseholdInfo 住户
     * @return 住户
     */
    @Override
    public List<CmHouseholdInfo> selectCmHouseholdInfoList(CmHouseholdInfo cmHouseholdInfo)
    {
        return cmHouseholdInfoMapper.selectCmHouseholdInfoList(cmHouseholdInfo);
    }

    /**
     * 新增住户
     * 
     * @param cmHouseholdInfo 住户
     * @return 结果
     */
    @Override
    public int insertCmHouseholdInfo(CmHouseholdInfo cmHouseholdInfo)
    {
        cmHouseholdInfo.setCreateTime(DateUtils.getTime());
        Long apartId = Long.valueOf(cmHouseholdInfo.getUserApartment());
        CmApartmentInfo cmApartmentInfo = cmApartmentInfoMapper.selectCmApartmentInfoById(apartId);
        if (ObjectUtils.isEmpty(cmApartmentInfo)){
            throw new RuntimeException("公寓不存在");
        }
        CmRoomInfo cmRoomInfo = cmRoomInfoMapper.selectCmRoomInfoByApartIdAndRoomNo(apartId, cmHouseholdInfo.getUserRoom());
        if (ObjectUtils.isEmpty(cmRoomInfo)){
            cmRoomInfo = new CmRoomInfo();
            cmRoomInfo.setApartId(String.valueOf(apartId));
            cmRoomInfo.setApartName(cmHouseholdInfo.getName());
            cmRoomInfo.setRoomNo(cmHouseholdInfo.getUserRoom());
            cmRoomInfo.setRoomFloor(cmHouseholdInfo.getUserFloor());
            int i = cmRoomInfoMapper.insertCmRoomInfo(cmRoomInfo);
            if (i != 1){
                throw new RuntimeException("人员新增房间有误");
            }
        }else {
            cmRoomInfo.setLegal(cmHouseholdInfo.getControlName());
            cmRoomInfo.setLegalCard(cmHouseholdInfo.getControlIdCard());
            cmRoomInfo.setLegalTel(cmHouseholdInfo.getControlTel());
            cmRoomInfoMapper.updateCmRoomInfo(cmRoomInfo);
        }
        return cmHouseholdInfoMapper.insertCmHouseholdInfo(cmHouseholdInfo);
    }

    /**
     * 修改住户
     * 
     * @param cmHouseholdInfo 住户
     * @return 结果
     */
    @Override
    public int updateCmHouseholdInfo(CmHouseholdInfo cmHouseholdInfo)
    {
        cmHouseholdInfo.setUpdateTime(DateUtils.getTime());

        Long apartId = Long.valueOf(cmHouseholdInfo.getUserApartment());
        CmApartmentInfo cmApartmentInfo = cmApartmentInfoMapper.selectCmApartmentInfoById(apartId);
        if (ObjectUtils.isEmpty(cmApartmentInfo)){
            throw new RuntimeException("公寓不存在");
        }
        CmRoomInfo cmRoomInfo = cmRoomInfoMapper.selectCmRoomInfoByApartIdAndRoomNo(apartId, cmHouseholdInfo.getUserRoom());
        if (ObjectUtils.isEmpty(cmRoomInfo)){
            cmRoomInfo = new CmRoomInfo();
            cmRoomInfo.setApartId(String.valueOf(apartId));
            cmRoomInfo.setApartName(cmHouseholdInfo.getName());
            cmRoomInfo.setRoomNo(cmHouseholdInfo.getUserRoom());
            cmRoomInfo.setRoomFloor(cmHouseholdInfo.getUserFloor());
            int i = cmRoomInfoMapper.insertCmRoomInfo(cmRoomInfo);
            if (i != 1){
                throw new RuntimeException("人员新增房间有误");
            }
        }else {
            cmRoomInfo.setLegal(cmHouseholdInfo.getControlName());
            cmRoomInfo.setLegalCard(cmHouseholdInfo.getControlIdCard());
            cmRoomInfo.setLegalTel(cmHouseholdInfo.getControlTel());
            cmRoomInfoMapper.updateCmRoomInfo(cmRoomInfo);
        }
        return cmHouseholdInfoMapper.updateCmHouseholdInfo(cmHouseholdInfo);
    }

    /**
     * 批量删除住户
     * 
     * @param ids 需要删除的住户主键
     * @return 结果
     */
    @Override
    public int deleteCmHouseholdInfoByIds(Long[] ids)
    {
        return cmHouseholdInfoMapper.deleteCmHouseholdInfoByIds(ids);
    }

    /**
     * 删除住户信息
     * 
     * @param id 住户主键
     * @return 结果
     */
    @Override
    public int deleteCmHouseholdInfoById(Long id)
    {
        return cmHouseholdInfoMapper.deleteCmHouseholdInfoById(id);
    }
}
